package com.csii.adhoc.controller.info;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.info.post.PostPo;
import com.csii.adhoc.info.post.PostVo;
import com.csii.adhoc.info.post.service.PostService;
import com.csii.adhoc.util.DateUtils;
import com.csii.adhoc.util.ParamCheckUtils;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @description: 职务控制层
 * @author: panChuanJiang
 * @create: 2021-01-28 10:48
 */
@RestController
@Slf4j
@RequestMapping("/post")
public class PostController {
    @Autowired
    private PostService postService;

    @RequestMapping("postMaintain")
    @OperationLog("职务维护")
    public APIResult departmentMaintain(@RequestBody PostVo postVo) {
        String userId = Optional.ofNullable(String.valueOf(SecurityUtils.getSubject().getPrincipal())).orElse("未知用户");
        postVo.setUpdatedBy(userId)
                .setUpdatedDt(DateUtils.getDateString("yyyyMMddHHmmss"));
        postService.postMaintain(postVo, postVo.getOptMode());
        return APIResult.successResult(null);
    }

    /**
     * 根据部门id查职务
     * @param postPo
     * @return
     */
    @RequestMapping("postQuery")
    public APIResult departmentQuery(@RequestBody PostPo postPo) {
        if (ParamCheckUtils.checkStringEmpty(postPo.getDepartId())){
            throw new ParamValidityException("id为空");
        }
        return APIResult.successResult(postService.selectListPost(postPo));
    }
}
